import React from 'react';
import {Form, Input, Button, message} from 'antd';
import axios from 'axios';
import StubPicker from '../../components/pickCity/StubPicker';
import {injectIntl} from 'react-intl';

const FormItem = Form.Item;

@Form.create()
class AddAccount extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            indeterminate: true,
            checkAll: false,
            stationCheckAuth: [],
            menuCheckAuth: []
        }
        this.handleSubmit = this.handleSubmit.bind(this);
        this.checkPassword = this.checkPassword.bind(this);
        this.checkLength = this.checkLength.bind(this);
    }

    cancel() {
        this.props.close();
    }

    formItemLayout = {
        labelCol: {span: 1, offset: 0},
        wrapperCol: {span: 5},
    };

    componentDidMount() {

    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                values.role = values.stubGroupId.currency;
                delete values.stubGroupId;
                axios.post('/api/corp/subAcc/add', {
                    ...values,
                }).then(res => {
                    if (res && res.code == 200) {
                        message.success('添加成功');
                        this.props.form.resetFields();
                        this.props.history.go(-1);
                    } else {
                        message.error('添加失败');
                    }
                });
            }
        })
    };

    checkPrice = (rule, value, callback) => {
        const common = this.props.intl.messages.common;
        if (value.currency) {
            callback();
            return;
        }
        callback(`${common.please}${common.selectRole}`);
    };

    /*----------登陆表单处理--------------*/
    validatePhone = (rule, value, callback) => {
        const common = this.props.intl.messages.common;
        let myreg = /^1[3|4|5|7|8|9][0-9]{9}$/;
        if (value && !myreg.test(value)) {
            callback(common.PleaseEnterCorrectPhone);
        } else {
            callback();
        }
    };

    /**
     *  验证两次输入的密码
     * @param rule
     * @param value
     * @param callback
     */
    checkLength(rule, value, callback) {
        const common = this.props.intl.messages.common;
        if (value && value.length < 6) {
            callback(common.cannot6less);
        } else {
            callback();
        }
    }

    /**
     *  验证两次输入的密码
     * @param rule
     * @param value
     * @param callback
     */
    checkPassword(rule, value, callback) {
        const form = this.props.form;
        const common = this.props.intl.messages.common;
        if (value && value !== form.getFieldValue('password')) {
            callback(common['2passwordInconsistent']);
        } else {
            callback();
        }
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const common = this.props.intl.messages.common;
        return (
            <div>
                <Form onSubmit={this.handleSubmit}>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('account', {
                            rules: [
                                {
                                    required: true,
                                    message: common.enterLoginNameByPhone
                                }, {
                                    validator: this.validatePhone
                                }
                            ]
                        })(<Input placeholder={common.loginName}/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('password', {
                            rules: [
                                {
                                    required: true,
                                    message: common.enterLoginPassword
                                }, {
                                    validator: this.checkLength
                                }
                            ]
                        })(<Input placeholder={common.loginPassword}/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('agePassword', {
                            rules: [
                                {
                                    required: true,
                                    message: common.enterLoginPasswordAgain6
                                }, {
                                    validator: this.checkPassword
                                }
                            ]
                        })(<Input placeholder={common.enterLoginPasswordAgain}/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('name', {
                            rules: [
                                {
                                    required: true,
                                    message: common.enterNameOfOperator
                                }
                            ]
                        })(<Input placeholder={common.enterNameOfOperator}/>)}
                    </FormItem>
                    <FormItem {...this.formItemLayout}
                    >
                        {getFieldDecorator(`stubGroupId`, {rules: [{validator: this.checkPrice}],initialValue: {currency: undefined}})(
                            <StubPicker url="/api/role/list/qry" placeholder={common.selectRole} mode={true}/>
                        )}
                    </FormItem>
                    <FormItem {...this.formItemLayout} >
                        {getFieldDecorator('remark')(<Input placeholder={common.remark}/>)}
                    </FormItem>
                    <FormItem wrapperCol={{span: 5, offset: 0}} style={{marginTop: 24}}>
                        <Button onClick={() => {
                            this.props.history.go(-1)
                        }} style={{width: "45%", marginRight: '10%'}}>{common.cancel}</Button>
                        <Button type="primary" htmlType="submit" style={{width: "45%"}}>{common.save}</Button>
                    </FormItem>
                </Form>
            </div>
        );
    }
}

export default injectIntl(AddAccount);